﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Web;
using System.IO;
using Common;
using System.Threading;

namespace CodeMain
{
    public partial class Main : Form
    {
        public SqlConnection con;
        public string conStr;
        public string[] tableNames;
        public Main()
        {
            InitializeComponent();
        } 
        private void Form1_Load(object sender, EventArgs e)
        {    
        }
        private void btn_connectServer_Click(object sender, EventArgs e)
        {
            try
            {
                con = SqlHelper.Conn(this.txt_userName.Text, txt_userPwd.Text, "", txt_server.Text);
                if (con.State.ToString() != "Open")
                {
                    con.Open();
                    MessageBox.Show("连接成功！");                   
                    string sql = "select name from master..sysdatabases";
                    DataSet ds = SqlHelper.ExecuteDataset(con, CommandType.Text, sql);
                    this.cbb_DataBase.DataSource = ds.Tables[0];
                    this.cbb_DataBase.DisplayMember = "name";
                    this.cbb_DataBase.ValueMember = "name";  
                }               
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }
        public bool bindTables(SqlConnection con)
        {
            listBox1.Items.Clear();
            con.Open();
            string sql = "select * from sysobjects where type='U'AND id<>'1977058079'";
            DataSet ds = SqlHelper.ExecuteDataset(con, CommandType.Text, sql);            
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    listBox1.Items.Add(dr["name"].ToString());                    
                }
                return true;
            }
            return false;
        }
        private void cbb_DataBase_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btn_select.Enabled = true;
            this.btn_select.Enabled = true;
            this.btn_AllCanel.Enabled = true;
            this.btn_canel.Enabled = true;
            this.listBox2.Items.Clear();
            this.listBox1.Items.Clear();
            string database=((DataRowView)cbb_DataBase.SelectedItem)["name"].ToString();
            if (database!="")
            {
                if(bindTables(SqlHelper.Conn(txt_userName.Text,txt_userPwd.Text,database,txt_server.Text)))
                {
                    conStr = "server=" + txt_server.Text + ";uid=" + txt_userName.Text + ";pwd=" + txt_userName.Text + ";database=" + database;
                }
                
            }
        }

        private void btn_AllSelect_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count!=0)
            {
                btn_AllCanel.Enabled = true;                
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    listBox2.Items.Add(listBox1.Items[i].ToString());
                }
                listBox1.Items.Clear();
                btn_AllSelect.Enabled = false;
                getTables();
            }
            
        }

        private void btn_AllCanel_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count!=0)
            {
                this.btn_AllSelect.Enabled = true;                
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    listBox1.Items.Add(listBox2.Items[i].ToString());
                }
                listBox2.Items.Clear();
                btn_AllCanel.Enabled = false;
                getTables();
            }            
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex>-1)
            {
                    listBox2.Items.Add(listBox1.SelectedItem);
                    listBox1.Items.Remove(listBox1.SelectedItem);
                    this.btn_AllCanel.Enabled = true;
                    this.btn_canel.Enabled = true;
            }
            else 
            {
                MessageBox.Show("没有选择移动的项！");
                if (listBox1.Items.Count==0)
                {
                    this.btn_select.Enabled = false;
                    this.btn_AllSelect.Enabled = false;
                }
            }
            getTables();
        }

        private void btn_canel_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex>-1)
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                listBox2.Items.Remove(listBox2.SelectedItem);
                this.btn_select.Enabled = true;
                this.btn_AllSelect.Enabled = true;
            }
            else
            {
                MessageBox.Show("没有选择移动的项！");
                if (listBox2.Items.Count==0)
                {
                    this.btn_canel.Enabled = false;
                    this.btn_AllCanel.Enabled = false;  
                }                              
            }
            getTables();
        }

         private void btn_Model_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
            {
                MessageBox.Show("请添加表！");
                return;
            }
            if (txt_ProgramName.Text=="")
            {
                MessageBox.Show("程序集不能为空！");
                return;
            }          
            MessageBox.Show(getCode.getModel(tableNames, conStr));
        }   
        private void btn_DAL_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count==0)
            {
                MessageBox.Show("请添加表！");
                return;
            }
            if (txt_ProgramName.Text == "")
            {
                MessageBox.Show("程序集不能为空！");
                return;
            }          
            MessageBox.Show(getCode.getDAL(tableNames,conStr,txt_ProgramName.Text));
        }

        #region 生成dal
        public void getDal() 
        {
            tableInfo tableInfor = new tableInfo();
            try
            {
                string path = Application.StartupPath;
                path = path.Substring(0, path.Length - 18) + "Code\\";
                path = path + "sqlDal\\";
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    
                    string str = "";//sql语句字段
                    string strParam = "";//sql语句参数

                    string k = tableInfor.getPrimaryKey(listBox2.Items[i].ToString(), conStr);
                    string table = listBox2.Items[i].ToString();
                    string modelName = table.ToLower() + "Model";
                    DirectoryInfo di = new DirectoryInfo(path);
                    DataSet ds = tableInfor.getTableInfo(table, conStr,"2000");
                    if (!di.Exists)
                    {
                        di.Create();
                    }                    
                    StreamWriter sm = new StreamWriter(path + table + "Dal.cs", false, Encoding.GetEncoding("UTF-8"));
                    sm.WriteLine("using System;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing " + this.txt_ProgramName.Text + ".Common;\r\nusing " + txt_ProgramName.Text + ".Data.Factory;\r\nusing System.Collections;\r\nusing System.Data;\r\nusing System.Data.SqlClient;");
                    sm.WriteLine("namespace " + txt_ProgramName.Text + ".Data");
                    sm.WriteLine("{");
                    sm.WriteLine("\tpublic class " + table + "Dal");
                    sm.WriteLine("\t{");
                    sm.WriteLine("\t\tIDBHelp db = DBHelp.Create();");
                    sm.WriteLine("\t\t Hashtable htSyn = Hashtable.Synchronized(new Hashtable());");
                    sm.WriteLine("\t\tStringBuilder strSql = new StringBuilder();");
                    sm.WriteLine();
                    #region "---是否存在该记录---"
                    sm.WriteLine("\t\t#region 是否存在该记录");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 是否存在该记录");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + k + "\">标识列ID</param>");
                    sm.WriteLine("\t\tpublic bool Exists(int " + k + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\thtSyn.Clear();");
                    sm.WriteLine("\t\t\tstrSql.Remove(0, strSql.Length);");
                    sm.WriteLine("\t\t\tstrSql.Append(\"select count(1) from [" + table + "] where " + k + "=@" + k.ToLower() + " \");");
                    sm.WriteLine("\t\t\thtSyn.Add(\"@" + k.ToLower() + "\", " + k + ");");
                    sm.WriteLine("\t\t\treturn db.IsExists(strSql.ToString(), htSyn);");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion "---end---"
                    sm.WriteLine();
                    #region "---得到该表数据列表---"
                    sm.WriteLine("\t\t#region 得到该表数据列表");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 得到该表数据列表");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\tpublic IList<" + table + "> GetModel()");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\thtSyn.Clear();");
                    sm.WriteLine("\t\t\tstrSql.Remove(0, strSql.Length);");
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        string text = ds.Tables[0].Rows[j]["columnName"].ToString();
                        string value = ds.Tables[0].Rows[j]["dataType"].ToString();
                        if (value != "sysname")
                        {
                            str += "[" + text + "],";
                        }
                        else
                            continue;
                       
                    }
                    str = str.Substring(0, str.Length - 1);
                    sm.WriteLine("\t\t\tstrSql.Append(\"Select " + str + " FROM [" + table + "]\");");
                    sm.WriteLine("\t\t\t return get" + table + "BySql(strSql.ToString());");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine();
                    #region "---根据标识列查询实体---"
                    sm.WriteLine("\t\t#region 根据标识列查询实体");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 根据标识列查询实体");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + k + "\">标识列ID</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic " + table + " GetModel(int " + k.ToLower() + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\thtSyn.Clear();");
                    sm.WriteLine("\t\t\tstrSql.Remove(0, strSql.Length);");
                    sm.WriteLine("\t\t\tstrSql.Append(\"select " + str + " FROM [" + table + "]\");");
                    sm.WriteLine("\t\t\tstrSql.Append(\" where " + k + "=@" + k.ToLower() + " \");");
                    sm.WriteLine("\t\t\thtSyn.Add(\"@" + k.ToLower() + "\", " + k.ToLower() + ");");
                    sm.WriteLine("\t\t\tDataTable dt = db.GetDataTable(strSql.ToString(), htSyn);");
                    sm.WriteLine("\t\t\t" + table + " " + modelName + " = new " + table + "();");
                    sm.WriteLine("\t\t\tif (dt.Rows.Count > 0)");
                    sm.WriteLine("\t\t\t{");
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        string text = ds.Tables[0].Rows[j]["columnName"].ToString();
                        string value = ds.Tables[0].Rows[j]["dataType"].ToString();
                        value = tableInfor.convertDB(value);
                        if (text != k)
                        {
                            if (value == "String")
                            {
                                sm.WriteLine("\t\t\t\t" + modelName + "." + text.Remove(1, text.Length - 1).ToLower() + text.Substring(1, text.Length - 1) + "=dt.Rows[0][\"" + text + "\"].ToString();");
                            }
                            else if(value!="")
                            {
                                sm.WriteLine("\t\t\t\t" + modelName + "." + text.Remove(1, text.Length - 1).ToLower() + text.Substring(1, text.Length - 1) + "=Convert.To" + value + "(dt.Rows[0][\"" + text + "\"]);");
                            }

                        }
                    }
                    sm.WriteLine("\t\t\t}");
                    sm.WriteLine("\t\t\treturn " + modelName + ";");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion "---end---"
                    sm.WriteLine();
                    #region "---修改信息---"
                    sm.WriteLine("\t\t#region 修改信息");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 修改信息");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + table + "\">实体</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic bool Update(" + table + " " + modelName + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\thtSyn.Clear();");
                    sm.WriteLine("\t\t\tstrSql.Remove(0, strSql.Length);");
                    sm.WriteLine("\t\t\tstrSql.Append(\"update [" + table + "] set \");");
                    str = "";
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        string text = ds.Tables[0].Rows[j]["columnName"].ToString();
                        string value = ds.Tables[0].Rows[j]["dataType"].ToString();
                        if (text != k&&value!="sysname")
                        {
                            str += "\t\t\tstrSql.Append(\"" + "[" + text + "]" + "=@" + text.ToLower() + ",\");\n";  
                        }
                    }
                    str = str.Remove(str.LastIndexOf(','), 1);                 
                    sm.WriteLine(str);
                    sm.WriteLine("\t\t\tstrSql.Append(\" where " + k + "=@" + k.ToLower() + " \");");
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        string text = ds.Tables[0].Rows[j]["columnName"].ToString();
                        string value = ds.Tables[0].Rows[j]["dataType"].ToString();
                        if (value != "sysname")
                        {
                            sm.WriteLine("\t\t\thtSyn.Add(\"@" + text.ToLower() + "\", " + modelName + "." + text.Remove(1, text.Length - 1).ToLower() + text.Substring(1, text.Length - 1) + ");");                            
                        }
                        else
                            continue;
                    }
                    sm.WriteLine("\t\t\tif (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)");
                    sm.WriteLine("\t\t\t{");
                    sm.WriteLine("\t\t\t\treturn true;");
                    sm.WriteLine("\t\t\t}");
                    sm.WriteLine("\t\t\treturn false;");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion "---end---"
                    sm.WriteLine();
                    #region "---删除信息---"
                    sm.WriteLine("\t\t#region 删除信息");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 删除信息");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + k + "\">标识列ID</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic bool Delete(int " + k + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\thtSyn.Clear();");
                    sm.WriteLine("\t\t\tstrSql.Remove(0, strSql.Length);");
                    sm.WriteLine("\t\t\tstrSql.Append(\"delete from [" + table + "] where " + k + "=@" + k.ToLower() + "\");");
                    sm.WriteLine("\t\t\thtSyn.Add(\"" + k.ToLower() + "\", " + k + ");");
                    sm.WriteLine("\t\t\tif (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)");
                    sm.WriteLine("\t\t\t{");
                    sm.WriteLine("\t\t\t\treturn true;");
                    sm.WriteLine("\t\t\t}");
                    sm.WriteLine("\t\t\treturn false;");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion "---end---"
                    sm.WriteLine();
                    #region -----添加信息------
                    sm.WriteLine("\t\t#region 添加信息");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 添加信息");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + table + "\">实体</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic bool Insert(" + table + " " + modelName + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\thtSyn.Clear();");
                    sm.WriteLine("\t\t\tstrSql.Remove(0, strSql.Length);");
                    str = "";
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {

                        string text = ds.Tables[0].Rows[j]["columnName"].ToString();
                        string value = ds.Tables[0].Rows[j]["dataType"].ToString();
                        if (k != text&&value!="sysname")
                        {
                            str += "[" + text + "],";
                            strParam += "@" + text.ToLower() + ",";
                        }

                    }
                    str = str.Substring(0, str.Length - 1);
                    strParam = strParam.Substring(0, strParam.Length - 1);
                    sm.WriteLine("\t\t\tstrSql.Append(\"insert into [" + table + "](" + str + ") values (" + strParam + ")\");");
                    for (int j = 0; j <ds.Tables[0].Rows.Count; j++)
                    {

                        string text = ds.Tables[0].Rows[j]["columnName"].ToString();
                        string value = ds.Tables[0].Rows[j]["dataType"].ToString();
                        if (k != text&&value!="sysname")
                        {
                            sm.WriteLine("\t\t\thtSyn.Add(\"@" + text.ToLower() + "\", " + modelName + "." + text.Remove(1, text.Length - 1).ToLower() + text.Substring(1, text.Length - 1) + ");");
                        }
                    }
                    sm.WriteLine("\t\t\tif (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)");
                    sm.WriteLine("\t\t\t{");
                    sm.WriteLine("\t\t\t\treturn true;");
                    sm.WriteLine("\t\t\t}");
                    sm.WriteLine("\t\t\treturn false;");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine();
                    #region ----根据sql语句获取集合
                    sm.WriteLine("\t\t#region 根据SQL语句获取集合");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t///根据SQL语句获取集合");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"strSql\">sql语句</param>");
                    sm.WriteLine("\t\tpublic IList<" + table + "> get" + table + "BySql(string strSql)");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\tIList<" + table + "> list = new List<" + table + ">();");
                    sm.WriteLine("\t\t\tDataTable dt = db.GetDataTable(strSql);");
                    sm.WriteLine("\t\t\tif (dt.Rows.Count > 0)");
                    sm.WriteLine("\t\t\t{");
                    sm.WriteLine("\t\t\t\tforeach (DataRow dr in dt.Rows)");
                    sm.WriteLine("\t\t\t\t{");
                    sm.WriteLine("\t\t\t\t\t" + table + " " + modelName + " = new " + table + "();");
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        string text = ds.Tables[0].Rows[j]["columnName"].ToString();
                        string value = ds.Tables[0].Rows[j]["dataType"].ToString();
                        value = tableInfor.convertDB(value);
                        if (value == "String")
                        {
                            sm.WriteLine("\t\t\t\t\t" + modelName + "." + text.Remove(1, text.Length - 1).ToLower() + text.Substring(1, text.Length - 1) + "=dr[\"" + text + "\"].ToString();");
                        }
                        else if(value!="")
                        {
                            sm.WriteLine("\t\t\t\t\t" + modelName + "." + text.Remove(1, text.Length - 1).ToLower() + text.Substring(1, text.Length - 1) + "= Convert.To" + value + "(dr[\"" + text + "\"]);");
                        }

                    }
                    sm.WriteLine("\t\t\t\t\tlist.Add(" + modelName + ");");
                    sm.WriteLine("\t\t\t\t}");
                    sm.WriteLine("\t\t\t}");
                    sm.WriteLine("\t\t\treturn list;");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine("\t}");
                    sm.WriteLine("}");
                    sm.Flush();
                    sm.Close();                   
                }
                MessageBox.Show("数据链路层生成成功！！路径为：" + path + " 下面");
            }
            catch (Exception)
            {

                MessageBox.Show("生成失败！"); ;
            }
            
        }
        #endregion

        private void btn_BLL_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
            {
                MessageBox.Show("请添加表！");
                return;
            }
            if (txt_ProgramName.Text == "")
            {
                MessageBox.Show("程序集不能为空！");
                return;
            }
            getBll();

        }
        #region 生成bll
        public void getBll() 
        {
            try
            {
                string path = Application.StartupPath;
                path = path.Substring(0, path.Length - 18) + "Code\\";
                path = path + "Bll\\";
                tableInfo tableinfo = new tableInfo();
                for (int j = 0; j < listBox2.Items.Count; j++)
                {
                    string key= tableinfo.getPrimaryKey(listBox2.Items[j].ToString(),conStr);
                    string table = listBox2.Items[j].ToString();                   
                    string dalName = table.Remove(1, table.Length - 1).ToLower() + "Dal";
                    DirectoryInfo di = new DirectoryInfo(path);
                    if (!di.Exists)
                    {
                        di.Create();
                    }                    
                    StreamWriter sm = new StreamWriter(path + table +"Bll"+".cs", false, Encoding.GetEncoding("UTF-8"));
                    sm.WriteLine("using System;");
                    sm.WriteLine("using System.Collections.Generic;");
                    sm.WriteLine("using System.Text;");
                    sm.WriteLine("using "+txt_ProgramName.Text+".Data;");
                    sm.WriteLine("namespace " + txt_ProgramName.Text + ".Business");
                    sm.WriteLine("{");
                    sm.WriteLine("\tpublic class "+table+"Bll");
                    sm.WriteLine("\t{");            
                    sm.WriteLine("\t\t" + table + "Dal " + dalName+ "=new " + table + "Dal();");
                    sm.WriteLine("");
                    #region ---是否存在该记录----
                    sm.WriteLine("\t\t#region 是否存在该记录");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 是否存在该记录");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\tpublic bool Exists(int " + key + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\treturn " + dalName + ".Exists(" + key + ");");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine("");
                    #region ---获得数据列表----
                    sm.WriteLine("\t\t#region 获得数据列表");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 获得数据列表");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\tpublic IList<" + table + "> GetModel()");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\treturn " + dalName + ".GetModel();");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine("");
                    #region ---根据标识列获得数据实体----
                    sm.WriteLine("\t\t#region 根据标识列获得数据实体");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 根据标识列获得数据实体");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + key + "\">标识列</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic " + table + " GetModel(int " + key + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\treturn " + dalName + ".GetModel(" + key + ");");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine("");
                    #region ---修改信息----
                    sm.WriteLine("\t\t#region 修改信息");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 修改信息");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + table + "\">实体</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic bool Update(" + table + " " + table.ToLower() + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\t return " + dalName + ".Update(" + table.ToLower() + ");");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine("");
                    #region ---删除信息----
                    sm.WriteLine("\t\t#region 删除信息");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 删除信息");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + key + "\">标识列</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic bool Delete(int " + key + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\t return " + dalName + ".Delete(" + key + ");");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine("");
                    #region ---添加信息----
                    sm.WriteLine("\t\t#region 添加信息");
                    sm.WriteLine("\t\t/// <summary>");
                    sm.WriteLine("\t\t/// 添加信息");
                    sm.WriteLine("\t\t/// </summary>");
                    sm.WriteLine("\t\t/// <param name=\"" + table + "\">实体</param>");
                    sm.WriteLine("\t\t/// <returns></returns>");
                    sm.WriteLine("\t\tpublic bool Insert(" + table + " " + table.ToLower() + ")");
                    sm.WriteLine("\t\t{");
                    sm.WriteLine("\t\t\t return " + dalName + ".Insert(" + table.ToLower() + ");");
                    sm.WriteLine("\t\t}");
                    sm.WriteLine("\t\t#endregion");
                    #endregion
                    sm.WriteLine("\t}");
                    sm.WriteLine("}");
                    sm.Flush();
                    sm.Close();
                }
                MessageBox.Show(this, "业务逻辑层生成成功！！路径为：" + path + " 下面");
            }
            catch (Exception)
            {
                
                MessageBox.Show("生成失败！");
            }
        }
        #endregion

        private void btn_TC_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
            {
                MessageBox.Show("请添加表！");
                return;
            }
            if (txt_ProgramName.Text=="")
            {
                MessageBox.Show("程序集不能为空！");
                return;
            }
            this.btn_Model_Click(null, null);
            getDal();
            getBll();
        }
        #region 得到生成表的数组
        public void getTables()
        {
            tableNames = new string[listBox2.Items.Count];
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                tableNames[i] = listBox2.Items[i].ToString();
            }
        }
        #endregion
    }
}